home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 6707 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: mail2news.demon.co.uk!ns.unibol.com
  2. From: John Girvin <jgirvin@bfs.unibol.com>
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Collision Detects !!!!
  5. Date: Sun, 31 Mar 1996 13:57:23 GMT
  6. Message-ID: <199603311357.NAA27594@mailhost.unibol.com>
  7. X-NNTP-Posting-Host: ns.unibol.com
  8. X-Newsreader: TIN [version 1.2 PL2]
  9. X-Mail2News-Path: ns.unibol.com
  10.  
  11. On Sat, 30 Mar 1996 17:31:02 GMT, breese@imada.ou.dk (Bjorn Reese) wrote:
  12. :>Cezary Kobylinski (amigos@safona.tuniv.szczecin.pl) wrote:
  13. :>> I want to know what is the best method to detect collision between any 
  14. :>> bobs ?!
  15.  
  16. :>Use coordinate check as a quick and approximate detection.
  17.  
  18. :>Assuming that (xpos,ypos) is the upper left corner of the bob,
  19. :>it could look something like this (must be done for each pair
  20. :>of a's an b's)
  21.  
  22. :>  if (a->flags.solid &&
  23. :>      (a->xpos < b->xpos + b->xsize) &&
  24. :>      (a->xpos + a->xsize > b->xpos ) &&
  25. :>      (a->ypos < b->ypos + b->ysize) &&
  26. :>      (a->ypos + a->ysize > b->ypos )) {
  27. :>    /* Overlap */
  28. :>  }
  29.  
  30. Theres a method that uses unsigned compares and requires only 2
  31. comparisons per object pair. It does require you to store or 
  32. calculate the coordinates of the bottom right hand corner of your
  33. bounding rectangle for *either* "a" objects or "b" objects, so
  34. the speedup may be minimal in some cases. However, for situations
  35. where you are checking if one "a" object (eg: player) has collided
  36. with loads of other objects (eg: enemy bullets) you can get a good
  37. speedup by pre-calcing the player BRHC outside the loop.
  38.  
  39. The pseudo code looks like this:
  40.  
  41. -------------------8<-----------------------------------------------
  42. px2 = player->x + player->width        // PreCalc player BRHC coords.
  43. py2 = player->y + player->height
  44.  
  45. for (i=1; i<num_bullets; i++)
  46. {
  47.     if ( (bullet[i]->x - px2) < (bullet[i]->width + player->width) &&
  48.          (bullet[i]->y - py2) < (bullet[i]->height + player->height) )
  49.     {
  50.     // Collision!
  51.     // Call your player_was_hit() function.
  52.     }
  53. }
  54. -------------------8<-----------------------------------------------
  55.  
  56. All comparisons and variables are ***UNSIGNED***. It doesnt work if you
  57. use signed arithmetic! 
  58.  
  59. Note that (bullet->width+player->width) and (bullet->width+player->width) 
  60. are constants inside the loop (best place for constants, IMHO ;) if all the 
  61. bullets are the same size. Obvious optimisation #1
  62.  
  63. A not-so-obvious optimisation depends on the shape of your playing
  64. area. If its tall and thin (eg: vertical scroller) then it is more
  65. likely that the Y-coords will be "out of range" than the X coords,
  66. so you should shift the Y comparison to be first in order to be more
  67. likely to discard non-collisions on the first comparison. Vice-versa
  68. if your playing area is wider than it is tall.
  69.  
  70. I have 68k code for many instances of this type of collision detection. 
  71. Mail me if you want it or would like it posted here, or if any of this
  72. rambling confuses you :)
  73.  
  74. Also, look for references on "sector based collision detection",
  75. especially around rec.games.programmer archives (HINT!). It might
  76. help you more if you have lots of objects to check.
  77.  
  78. Well, I hope that 0.02c helped!
  79.  
  80. cya,
  81. /John.
  82. __________________________________________________________________________
  83. |/\John Girvin : developing software for Unibol Inc., speaking for myself|
  84. |\/jgirvin@bfs.unibol.com | Amiga,!PC,net,Trek,SF,MTB,C2H50H,house,techno|
  85. |girv@girvnet.demon.co.uk | Youll never take me alive, Macro$loth fiends!|
  86. \A1200/030-40/10M/3.0 A500/000-7/2M/2.04 464/Z80-4/0.0625M/1.0 Team AMIGA/
  87.